package com.qianfeng.openplatform.webmaster.service.impl;

import com.qianfeng.openplatform.webmaster.mapper.MenuMapper;
import com.qianfeng.openplatform.webmaster.pojo.Menu;
import com.qianfeng.openplatform.webmaster.pojo.MenuExample;
import com.qianfeng.openplatform.webmaster.service.MenuService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 江江
 * @Date 2020/7/22 16:22
 */
@Service
@Transactional //事务
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    @Transactional(readOnly = true)
    public List<Menu> getLeftMenu() {
        List<Menu> menuLeftList = new ArrayList<>();
        List<Menu> menuList = getMenuAll();
        for (Menu menu : menuList) {
            List<Menu> childerList = new ArrayList<>();

            for (Menu m : menuList) {
                System.out.println(menu.toString());
                System.out.println("childerList.add(menu.getId()): "+menu.getId().toString());
                if (m.getParentId() != null && m.getParentId() == menu.getId()&& m.getType()==1) {
//                if (m.getParentId() != null &&  m.getType()==1) {
                    System.out.println(m.toString());
                    System.out.println("childerList.add(m.getParentId()): "+m.getParentId().toString());


                    childerList.add(m);
                }
            }
            menu.setChildren(childerList);
            if (menu.getType() != 2 && menu.getParentId()==null) {
                 System.out.println("menuLeftList.add(menu);: "+menu.toString());
                menuLeftList.add(menu);
            }
        }
        return menuLeftList;
    }

    private List<Menu> getMenuAll() {
        MenuExample example = new MenuExample();
        List<Menu> menuList = menuMapper.selectByExample(example);
        return menuList;
    }
}
